/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * by the @author tags. See the COPYRIGHT.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.netty.handler.codec.bayeux;

/**
 * After a Bayeux client has discovered the server's capabilities with a
 * handshake exchange, a connection is established by sending a message to the
 * "/meta/connect" channel. This message may be transported over any of the
 * transports indicated as supported by the server in the handshake response.
 *
 * A connect request MUST contain the message fields:
 *   channel
 *     value "/meta/connect"
 *   clientId
 *     The client ID returned in the handshake response
 *   connectionType
 *     The connection type used by the client for the purposes of this
 *     connection.
 *
 * A connect request MAY contain the message fields:
 *   ext
 *   id
 *
 * A client MAY send other messages in the same HTTP request with a connection
 * message. A server MUST handle any other message sent in the same request as a
 * connect message after the handling of the connect message is complete.
 *
 * An example connect request is:
 * [
 *   {
 *      "channel": "/meta/connect",
 *      "clientId": "Un1q31d3nt1f13r",
 *      "connectionType": "long-polling"
 *    }
 * ]
 *
 * A transport MUST maintain one and only one outstanding connect message. When
 * a HTTP response that contains a /meta/connect response terminates, the client
 * MUST wait at least the interval specified in the last received advice before
 * following the advice to reestablish the connection
 *
 * @author daijun
 */
public class ConnectRequest extends BayeuxMessage implements BayeuxInterface {

    //required
    //private String channel;
    //private String clientId;
    //private String connectionType;
    //optional
    //private BayeuxExt ext;
    //private String id;
    public ConnectRequest(BayeuxMessage bayeux) {
        this.channel = "/meta/connect";
        this.clientId = bayeux.getClientId();
        this.connectionType = bayeux.getConnectionType();
        this.ext = bayeux.ext;
        this.id = bayeux.id;
    }

    @Override
    public boolean isValid() {
        if (channel == null || channel.trim().length() == 0 || !channel.trim().equalsIgnoreCase("/meta/connect")) {
            return false;
        }else if(clientId == null || clientId.trim().length() == 0){
            return false;
        }else if(connectionType == null || connectionType.trim().length() == 0){
            return false;
        }
        return true;
    }

    static boolean isValid(BayeuxMessage bayeux) {
        return new ConnectRequest(bayeux).isValid();
    }
}
